#!/bin/bash

TEMP=$(getopt -o '' --long 'sysfw:,sysfwdata:,dmdata:,out:,sbl:,key:,innerdata:' -n 'k3img' -- "$@")

if [ $? -ne 0 ]; then
	echo 'Terminating...' >&2
	exit 1
fi

# Note the quotes around "$TEMP": they are essential!
eval set -- "$TEMP"
unset TEMP

while true; do
        case "$1" in
        '--sysfw')
		sysfw="$2"
		shift 2
		continue
	;;
        '--sysfwdata')
		sysfwdata="$2"
		shift 2
		continue
	;;
        '--sysfw')
		sysfw="$2"
		shift 2
		continue
	;;
        '--dmdata')
		dmdata="$2"
		shift 2
		continue
	;;
	'--out')
		out="$2"
		shift 2
		continue
	;;
	'--sbl')
		sbl="$2"
		shift 2
		continue
	;;
	'--key')
		key="$2"
		shift 2
		continue
	;;
	'--innerdata')
		innerdata="$2"
		shift 2
		continue
	;;
	'--')
		shift
		break
	;;
	*)
		echo 'Internal error!' >&2
		exit 1
	;;
	esac
done

shasbl=$(sha512sum $sbl | sed 's/ .*//')
shasysfw=$(sha512sum $sysfw | sed 's/ .*//')
shasysfwdata=$(sha512sum $sysfwdata | sed 's/ .*//')
shadmdata=$(sha512sum $dmdata | sed 's/ .*//')

sblsize=$(stat -c%s $sbl)
sysfwsize=$(stat -c%s $sysfw)
sysfwdatasize=$(stat -c%s $sysfwdata)
dmdatasize=$(stat -c%s $dmdata)

total=$(($sblsize + $sysfwsize + $sysfwdatasize + $dmdatasize))

certcfg=$(mktemp k3img.XXXXXXX)
cert=$(mktemp k3img.XXXXXXX)

num_comp=4

if [ -n "${innerdata}" ]; then
	shainnerdata=$(sha512sum $innerdata | sed 's/ .*//')
	innerdatasize=$(stat -c%s $innerdata)

	innercert=$(cat <<EOF
[sysfw_inner_cert]
compType = INTEGER:3
bootCore = INTEGER:0
compOpts = INTEGER:0
destAddr = FORMAT:HEX,OCT:00000000
compSize = INTEGER:$innerdatasize
shaType  = OID:2.16.840.1.101.3.4.2.3
shaValue = FORMAT:HEX,OCT:$shainnerdata
EOF
)

	num_comp=$((num_comp + 1))
	total=$((total + innerdatasize))
	sysfw_inner_cert="sysfw_inner_cert=SEQUENCE:sysfw_inner_cert"
fi

cat > $certcfg <<EndOfHereDocument
[ req ]
distinguished_name     = req_distinguished_name
x509_extensions        = v3_ca
prompt                 = no
dirstring_type         = nobmp

[ req_distinguished_name ]
C                      = US
ST                     = TX
L                      = Dallas
O                      = Texas Instruments Incorporated
OU                     = Processors
CN                     = TI Support
emailAddress           = support@ti.com

[ v3_ca ]
basicConstraints = CA:true
1.3.6.1.4.1.294.1.3=ASN1:SEQUENCE:swrv
1.3.6.1.4.1.294.1.9=ASN1:SEQUENCE:ext_boot_info
1.3.6.1.4.1.294.1.8=ASN1:SEQUENCE:debug

[swrv]
swrv=INTEGER:1

[ext_boot_info]
extImgSize=INTEGER:$total
numComp=INTEGER:$num_comp
sbl=SEQUENCE:sbl
sysfw=SEQUENCE:sysfw
sysfw_data=SEQUENCE:sysfw_data
$sysfw_inner_cert
dm_data=SEQUENCE:dm_data

[sbl]
compType = INTEGER:1
bootCore = INTEGER:16
compOpts = INTEGER:0
destAddr = FORMAT:HEX,OCT:43c00000
compSize = INTEGER:$sblsize
shaType  = OID:2.16.840.1.101.3.4.2.3
shaValue = FORMAT:HEX,OCT:$shasbl

[sysfw]
compType = INTEGER:2
bootCore = INTEGER:0
compOpts = INTEGER:0
destAddr = FORMAT:HEX,OCT:00040000
compSize = INTEGER:$sysfwsize
shaType  = OID:2.16.840.1.101.3.4.2.3
shaValue = FORMAT:HEX,OCT:$shasysfw

[sysfw_data]
compType = INTEGER:18
bootCore = INTEGER:0
compOpts = INTEGER:0
destAddr = FORMAT:HEX,OCT:00067000
compSize = INTEGER:$sysfwdatasize
shaType  = OID:2.16.840.1.101.3.4.2.3
shaValue = FORMAT:HEX,OCT:$shasysfwdata

[ debug ]
debugUID = FORMAT:HEX,OCT:0000000000000000000000000000000000000000000000000000000000000000
debugType = INTEGER:4
coreDbgEn = INTEGER:0
coreDbgSecEn = INTEGER:0

$innercert

[dm_data]
compType = INTEGER:17
bootCore = INTEGER:16
compOpts = INTEGER:0
destAddr = FORMAT:HEX,OCT:43c3a800
compSize = INTEGER:$dmdatasize
shaType  = OID:2.16.840.1.101.3.4.2.3
shaValue = FORMAT:HEX,OCT:$shadmdata

EndOfHereDocument

openssl req -new -x509 -key $key -nodes -outform DER -out $cert -config $certcfg -sha512

cat $cert $sbl $sysfw $sysfwdata $innerdata $dmdata > $out
